/*
 * @Date: 2020-08-25 16:25:30
 * @LastEditors: xujianxing
 * @LastEditTime: 2020-08-25 17:04:27
 * @FilePath: \nili\src\common\guard\auth.guard.ts
 */
import {
  CanActivate,
  ExecutionContext,
  Injectable,
  HttpException,
  BadRequestException,
  ForbiddenException,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { Request } from 'express';

const validateRequest = (req: Request) => {
  const token = req.headers['token'];
  if (token != 'xu') {
    throw new HttpException('token error', 405);
  }
  return true;
};

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const request = context.switchToHttp().getRequest();
    return validateRequest(request);
  }
}
